/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sheep.JUNG.utils;

import java.awt.Color;
import java.awt.Paint;
import java.awt.Shape;

import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.graph.decorators.AbstractVertexShapeFunction;
import edu.uci.ics.jung.graph.decorators.EdgePaintFunction;
import edu.uci.ics.jung.graph.decorators.VertexAspectRatioFunction;
import edu.uci.ics.jung.graph.decorators.VertexPaintFunction;
import edu.uci.ics.jung.graph.decorators.VertexSizeFunction;
import edu.uci.ics.jung.visualization.Layout;
import sheep.controller.Workspace;
import sheep.model.Task;
import sheep.model.graph.OurVertex;
/**
 *
 * @author geek
 */
public class OurVertexShape extends AbstractVertexShapeFunction implements EdgePaintFunction, VertexSizeFunction, 
        VertexAspectRatioFunction,VertexPaintFunction{
 
	//////////////////////////////////////////////////////////////////////////////////////	
	
	/**
	 * 
	 */
	Layout     layout ;
	Workspace workSpace;
	//////////////////////////////////////////////////////////////////////////////////////		
	
        Color lightBlue = new Color(130,130,255);
        Color blue = new Color(50,50,255);
        Color lightRed = new Color(255,130,130);
        Color red = new Color(255,50,50);
        
        
	/**
	 * @param layout
	 */
	public OurVertexShape(	Layout     layout , Workspace workSpace){
          this.workSpace = workSpace;
		setSizeFunction(this);
		setAspectRatioFunction(this);
		this.layout = layout;
	}

	//////////////////////////////////////////////////////////////////////////////////////		
	
	/* (non-Javadoc)
	 * @see edu.uci.ics.jung.graph.decorators.VertexSizeFunction#getSize(edu.uci.ics.jung.graph.Vertex)
	 */
  @Override
	public int getSize(Vertex vertex){
 		if (vertex instanceof OurVertex){
			if(((OurVertex)vertex).getTask().getName().equals("start") ||
                           ((OurVertex)vertex).getTask().getName().equals("end")     )
				return (int)(20);
		}  
		return (int)(40);
	}

	//////////////////////////////////////////////////////////////////////////////////////		
	
	/* (non-Javadoc)
	 * @see edu.uci.ics.jung.graph.decorators.VertexAspectRatioFunction#getAspectRatio(edu.uci.ics.jung.graph.Vertex)
	 */
  @Override
	public float getAspectRatio(Vertex vertex){
		return 1.0f;
	}

	//////////////////////////////////////////////////////////////////////////////////////		
	
	/* (non-Javadoc)
	 * @see edu.uci.ics.jung.graph.decorators.VertexShapeFunction#getShape(edu.uci.ics.jung.graph.Vertex)
	 */
  @Override
	public Shape getShape(Vertex vertex){
		if (vertex instanceof OurVertex){
			if(((OurVertex)vertex).getTask().getName().equals("start") ||
                           ((OurVertex)vertex).getTask().getName().equals("end")     )
				return factory.getRegularStar(vertex, 5);
		}
                if(((OurVertex)vertex).getTask().isDetailled())
                  return factory.getRegularStar(vertex, 10);
                          
		return factory.getEllipse(vertex);
	}

	//////////////////////////////////////////////////////////////////////////////////////		
 
	/* (non-Javadoc)
	 * @see edu.uci.ics.jung.graph.decorators.VertexPaintFunction#getDrawPaint(edu.uci.ics.jung.graph.Vertex)
	 */
  @Override
	public Paint getDrawPaint(Vertex o){ 
          OurVertex vertex = (OurVertex)o;
          if(vertex.getTask().getName().equals("start") ||
             vertex.getTask().getName().equals("end")     )
              return Color.BLACK; 

          if (workSpace.getSelectedTask()!= null)
            if (vertex.getTask() == workSpace.getSelectedTask())
              return Color.BLACK;

          if(vertex.getTask().isCritical()){
            if(vertex.getTask().isDetailled())
              return lightRed;
            else
              return red;
          }
          else{
            if(vertex.getTask().isDetailled())
              return lightBlue;
            else
              return blue;
          }
	} 
 
	//////////////////////////////////////////////////////////////////////////////////////		
 
	/* (non-Javadoc)
	 * @see edu.uci.ics.jung.graph.decorators.VertexPaintFunction#getFillPaint(edu.uci.ics.jung.graph.Vertex)
	 */
  @Override
	public Paint getFillPaint(Vertex o) {
          OurVertex vertex = (OurVertex)o;
          if((vertex).getTask().getName().equals("start") ||
            (vertex).getTask().getName().equals("end")     )
            return Color.LIGHT_GRAY; 

          if(vertex.getTask().isCritical()){
            if(((OurVertex)vertex).getTask().isDetailled())
              return lightRed;
            else
              return red;
          }
          else{
            if(((OurVertex)vertex).getTask().isDetailled())
              return lightBlue;
            else
              return blue;
          }
	}

	//////////////////////////////////////////////////////////////////////////////////////		

	/* (non-Javadoc)
	 * @see edu.uci.ics.jung.graph.decorators.EdgePaintFunction#getDrawPaint(edu.uci.ics.jung.graph.Edge)
	 */
  @Override
	public Paint getDrawPaint(Edge e) {
          //if (e.getEndpoints().getFirst() instanceof OurVertex){
            if(((OurVertex)e.getEndpoints().getFirst()).getTask().getName().equals("start") ||
               ((OurVertex)e.getEndpoints().getSecond()).getTask().getName().equals("end")     )
                  return new Color(220,220,220); 
          //}
          if(((OurVertex)e.getEndpoints().getFirst()).getTask().isCritical() &&
                  ((OurVertex)e.getEndpoints().getSecond()).getTask().isCritical())
            return Color.RED;
          else
            return Color.BLACK;     	
	}

	//////////////////////////////////////////////////////////////////////////////////////		

	/* (non-Javadoc)
	 * @see edu.uci.ics.jung.graph.decorators.EdgePaintFunction#getFillPaint(edu.uci.ics.jung.graph.Edge)
	 */
  @Override
	public Paint getFillPaint(Edge arg0) {
		if (arg0.getEndpoints().getFirst() instanceof OurVertex){
			if(((OurVertex)arg0.getEndpoints().getFirst()).getTask().getName().equals("start") ||
                           ((OurVertex)arg0.getEndpoints().getSecond()).getTask().getName().equals("end")     )
				return new Color(220,220,220);
		}  
		return Color.BLACK;  
	}
	 
	//////////////////////////////////////////////////////////////////////////////////////		
 
}

//////////////////////////////////////////////////////////////////////////////////////		
